Imports ComprUtils
Imports System.Collections.Generic
Imports System.ComponentModel

' simple wrapper class for Unzipping by Philip
Public Class MassiveUncompress : Inherits BackgroundWorker
  Private _sourceDir As String
  Public Property SourceDir() As String
    Get
      Return _sourceDir
    End Get
    Set(ByVal value As String)
      _sourceDir = value
    End Set
  End Property

  Private _targetDir As String
  Public Property TargetDir() As String
    Get
      Return _targetDir
    End Get
    Set(ByVal value As String)
      _targetDir = value
    End Set
  End Property

  Private _unzip As UnZip
  Public Property Unzip() As UnZip
    Get
      Return _unzip
    End Get
    Set(ByVal value As UnZip)
      _unzip = value
    End Set
  End Property
  Private _unrar As UnRar
  Public Property UnRar() As UnRar
    Get
      Return _unrar
    End Get
    Set(ByVal value As UnRar)
      _unrar = value
    End Set
  End Property
  Public Function GetUnPack(ByVal file As String) As IUnpack
    If file.EndsWith("zip") Then : Return _unzip
    ElseIf file.EndsWith("rar") Then : Return _unrar
    Else : Return Nothing
    End If
  End Function

  Private Sub fileunpacked_RunWorkerCompleted( _
    ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
  End Sub
  Protected Overrides Sub OnDoWork(ByVal e As System.ComponentModel.DoWorkEventArgs)
    Me.FindAllInDir()
    ''While _iFile < _packList.Count
    ''  System.Threading.Thread.Sleep(50)
    ''End While
    For iFile As Integer = 0 To _packList.Count - 1
      Dim currFile As String = _packList(iFile)
      Dim up As IUnpack = Me.GetUnPack(currFile)
      up.TargetDir = Me.TargetDir
      up.PackFilename = currFile
      up.ExtractPack()
      Dim perc As Integer = Convert.ToInt32(iFile * 100 / _packList.Count)
      Me.ReportProgress(perc, currFile)
    Next
    MyBase.OnDoWork(e)
  End Sub
  Private _testMode As Boolean
  Public Property TestMode() As Boolean
    Get
      Return _testMode
    End Get
    Set(ByVal value As Boolean)
      _testMode = value
    End Set
  End Property

#Region " recursive unpack... not used now "
  Sub ExtractAll(ByVal e As DoWorkEventArgs)
    Dim d1, d2 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    d2 = New IO.DirectoryInfo(_targetDir)
    _iUnzip = 0
    extrAllRecursive(d1, d2, e)
  End Sub

  Dim _iUnzip As Integer
  Private Sub extrAllRecursive(ByVal d1 As IO.DirectoryInfo, _
    ByVal d2 As IO.DirectoryInfo, ByVal e As DoWorkEventArgs)
    Dim fi As IO.FileInfo, subd1, subd2 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If CancellationPending Then
        e.Cancel = True
        Exit Sub
      End If
      If fi.Extension = ".zip" Then
        'increase decompress counter
        _iUnzip += 1
        Dim pctProg As Integer = (_iUnzip * 100) / PackList.Count
        ReportProgress(pctProg, fi.FullName)
        ' extract zip or sleep for a while
        If Not _testMode Then
          'Extract(fi.FullName, d2.FullName, fi.Extension.ToLower())
        Else : System.Threading.Thread.Sleep(50)
        End If
      End If
    Next
    For Each subd1 In d1.GetDirectories()
      subd2 = d2.CreateSubdirectory(subd1.Name)
      extrAllRecursive(subd1, subd2, e)
    Next
  End Sub
#End Region
  ' delete alll zips in directory
  Sub DeleteAllInDir()
    Dim d1 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    delAllRec(d1)
  End Sub
  Private Sub delAllRec(ByVal d1 As IO.DirectoryInfo)
    Dim fi As IO.FileInfo, subd1 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If fi.Extension = ".zip" OrElse fi.Extension = ".rar" Then
        If Not _testMode Then fi.Delete()
      End If
    Next
    For Each subd1 In d1.GetDirectories()
      delAllRec(subd1)
    Next
  End Sub
  ' find all zips in directory
  Dim _packList As New List(Of String)
  Public ReadOnly Property PackList() As List(Of String)
    Get
      Return _packList
    End Get
  End Property

  Sub FindAllInDir()
    Dim d1 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    _packList.Clear()
    findAllRec(d1)
  End Sub
  Private Sub findAllRec(ByVal d1 As IO.DirectoryInfo)
    Dim fi As IO.FileInfo, subd1 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If fi.Extension = ".zip" OrElse fi.Extension = ".rar" Then
        _packList.Add(fi.FullName)
      End If
    Next
    For Each subd1 In d1.GetDirectories()
      findAllRec(subd1)
    Next
  End Sub
End Class

'MFC-like wait cursor!
Class WaitCursor
  Implements IDisposable

  Private disposedValue As Boolean = False    ' To detect redundant calls

  ' IDisposable
  Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
      If disposing Then
        ' TODO: free unmanaged resources when explicitly called
      End If

      ' TODO: free shared unmanaged resources
      _frm.Cursor = Cursors.Default
    End If
    Me.disposedValue = True
  End Sub

#Region " IDisposable Support "
  ' This code added by Visual Basic to correctly implement the disposable pattern.
  Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
  End Sub
#End Region

  Dim _frm As Form
  Public Sub New(ByVal frm As Form)
    _frm = frm
    _frm.Cursor = Cursors.WaitCursor
  End Sub
End Class